<?php

class Peer extends CActiveRecord {
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'peers';
	}

	public function relations() {
		return array(
			'torrent'=>array(self::BELONGS_TO, 'Torrent', 'torrent_id'),
			'user'=>array(self::BELONGS_TO, 'User', 'user_id'),
		);

	}
	public function rules() {
		return array(
			array('peer_id, torrent_id, user_id', 'required'),
		);
	}

	public function safeAttributes() {
		return array('torrent_id', 'peer_id', 'ip', 'port', 'uploaded', 'downloaded', 'left', 'seeder', 'user_id');
	}

	public function random($limit) {
		$this->getDbCriteria()->mergeWith(array(
			'order' => 'RAND()',
			'limit' => $limit,
		));
		return $this;
	}

	public function isSeeder() {
		return $this->seeder === 'yes';
	}

}

?>
